package pw.hais.etgsh.dao;

import com.squareup.okhttp.Response;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;


import pw.hais.etgsh.model.ArticleModel;
import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;

/**
 * 采集 http://www.neihan8.com
 * Created by Hais1992 on 2015/12/16.
 */
public class ArticleDao {
    /**
     * 获取列表
     */
    public static void getList(String url,final String page, final OnHttpListener<List<ArticleModel>> listener) {
        String article_url = url.substring(0,url.length()-6) + page + ".html";
        Http.get(article_url, null, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String html) {
                doArticleHtmlToListModel(html, listener);
            }
        });
    }

    private static void doArticleHtmlToListModel(String html, OnHttpListener<List<ArticleModel>> listener) {
        List<ArticleModel> list = new ArrayList<>();
        Document document = Jsoup.parse(html);
        try {
            //当无翻页内容的时候 getElementsByClass("list") 会 空
            Elements elements = document.getElementsByClass("list").last().select("li");
            for (Element e : elements) {
                Element title_element = e.select("h3").select("a").last();
                String id = title_element.attr("href");
                String title = title_element.attr("title");
                e.select("span").remove();
                e.select("h3").remove();
                String content = e.text();
                list.add(new ArticleModel(id, title, content));
            }
        }catch (Exception e){e.printStackTrace();}
        if (listener != null) listener.onSuccess(null, list);
    }


    /**
     * 获取内容详情
     */
    public static void getModelById(final String id, final OnHttpListener<ArticleModel> listener) {
        Http.get(id, null, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String html) {
                doArticleHtmlToItemModel(id, html, listener);
            }
        });

    }

    public static void doArticleHtmlToItemModel(String id, String html, OnHttpListener<ArticleModel> listener) {
        Document document = Jsoup.parse(html);
        document.getElementsByClass("bdsharebuttonbox").remove();
        document.select("strong").remove();
        document.select("script").remove();

        ArticleModel article = new ArticleModel();

        if ("".equals(document.getElementsByClass("dede_pages").text())) {
            document.getElementsByClass("dede_pages").remove();
        } else {
            Elements elements = document.getElementsByClass("dede_pages").select("a");
            article.urls = new ArrayList<>();
            for (int i = 0; i < elements.size()-1; i++) {
                String url = elements.get(i).attr("href");
                if (!"".equals(url) && !"#".equals(url)) {
                    article.urls.add(id.substring(0,id.length()-12) + "/"+url);
                }
            }
        }
        document.getElementsByClass("dede_pages").remove();
        article.id = id;
        article.title = document.select("h1").text();
        article.content = document.getElementsByClass("content").html();

        listener.onSuccess(null, article);
    }

}
